home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / thomas / thomas.lha / Thomas / Thomas-1.1 / examples-from-book.text < prev    next >
Text File  |  1992-09-25  |  28KB  |  1,335 lines

  1. -*-Indented-Text-*-
  2.  
  3. This file contains the examples, taken directly from the Dylan manual.
  4. This file is not in an executable format.
  5.  
  6.  
  7. Page 27
  8.  
  9.    ? "abc"
  10.    "abc"
  11.    ? 123
  12.    123
  13.    ? foo:
  14.    foo:
  15.    ? #\a
  16.    #\a
  17.    ? #t
  18.    #t
  19.    ? #f
  20.    #f
  21.    ? (quote foo)
  22.    foo
  23.    ? 'foo
  24.    foo
  25.    ? '(1 2 3)
  26.    (1 2 3)
  27.  
  28.  
  29. Page 28-29
  30.  
  31.    ? <window>
  32.    {the class <window>}
  33.    ? concatenate
  34.    {the generic function concatenate}
  35.    ? (define my-variable 25)
  36.    my-variable
  37.    ? my-variable
  38.    25
  39.    ? (bind ((x 50))
  40.        (+ x x))
  41.    100
  42.    ? (setter element)
  43.    {the generic function (setter element)}
  44.    ? (define (setter my-variable) 20) 
  45.    (setter my-variable)
  46.    ? (setter my-variable)
  47.    20
  48.  
  49. Page 29
  50.  
  51.    ? (+ 3 4)
  52.    7
  53.    ? (* my-variable 3)
  54.    75
  55.    ? (* (+ 3 4) 5)
  56.    35
  57.    ? ((if #t + *) 4 5)
  58.    9
  59.  
  60. Page 30
  61.  
  62.    ; Creates and initializes a module variable
  63.    (define my-variable 25)
  64.    ; Sets the value to 12
  65.    (set! my-variable 12)
  66.    ; Returns 30. Uses lexical variables x and y.
  67.    (bind ((x 10) (y 20))
  68.       (+ x y))
  69.    ; Creates an anonymous method, which expects 2 
  70.    ; numeric arguments.
  71.    (method ((a <number>) (b <number>))
  72.       (list (- a b) (+ a b)))
  73.  
  74. Page 30
  75.  
  76.    ? (values 1 2 3)
  77.    1
  78.    2
  79.    3
  80.    ? (define-method edges ((center <number>)(radius <number>))
  81.        (values (- center radius) (+ center radius)))
  82.    edges
  83.    ? (edges 100 2)
  84.    98
  85.    102
  86.  
  87. Page 32
  88.  
  89.    ? foo
  90.    error: unbound variable foo
  91.    ? (define foo 10)
  92.    foo
  93.    ? foo
  94.    10
  95.    ? (+ foo 100)
  96.    110
  97.    ? bar
  98.    error: unbound variable bar
  99.    ? (define bar foo)
  100.    bar
  101.    ? bar
  102.    10
  103.    ? (define foo 20)
  104.    warning: redefining variable foo
  105.    ? foo
  106.    20
  107.    ? bar
  108.    10
  109.    ? (+ foo bar)
  110.    30
  111.  
  112. Page 33
  113.  
  114.    ? (bind ((number1 20))
  115.             (number2 30))
  116.        (+ number1 number2))
  117.    50
  118.  
  119. Page 33
  120.  
  121.    ? (bind ((x 20)
  122.             (y (+ x x)))
  123.        (+ y y))
  124.    80
  125.  
  126. Page 33
  127.  
  128.    ? (define foo 10)
  129.    foo
  130.    ? (+ foo foo)
  131.    20
  132.    ? (bind ((foo 35))
  133.        (+ foo foo))
  134.    70
  135.    ? (bind ((foo 20))
  136.        (bind ((foo 50))
  137.          (+ foo foo)))
  138.    100
  139.  
  140. Page 34
  141.  
  142.    ? (bind (((x <integer>) (sqrt 2)))
  143.            x)
  144.    error: 1.4142135623730951 is not an instance of <integer>
  145.  
  146.  
  147. Page 34
  148.  
  149.    ? (bind ((foo bar baz (values 1 2 3)))
  150.        (list foo bar baz))
  151.    (1 2 3)
  152.    ? (define-method opposite-edges ((center <number>)
  153.                                     (radius <number>))
  154.        (bind ((min max (edges center radius)))
  155.          (values max min)))
  156.    opposite-edges
  157.    ? (opposite-edges 100 2)
  158.    102
  159.    98
  160.  
  161. Page 34
  162.  
  163.    ? (bind ((x 10)
  164.             (y 20))
  165.        (bind ((x y (values y x)))
  166.          (list x y)))
  167.    (20 10)
  168.  
  169. Page 34
  170.  
  171.    ? (bind ((#rest nums (edges 100 2)))
  172.        nums)
  173.    (98 102)
  174.  
  175. Page 41
  176.  
  177.    ? (double 10)
  178.    error: unbound variable double.
  179.  
  180. Page 41
  181.  
  182.    ? (define-method double ((thing <number>))
  183.        (+ thing thing))
  184.    double
  185.    ? double
  186.    {the generic function double}
  187.    ? (double 10)
  188.    20
  189.  
  190. Page 41
  191.  
  192.    ? (double "the rain in Spain.")
  193.    error: no method for {the generic function double} was found
  194.           for the arguments ("the rain in Spain.")
  195.  
  196. Page 41
  197.  
  198.    ? (define-method double ((thing <sequence>))
  199.        (concatenate thing thing))
  200.    double
  201.    ? (double "the rain in Spain.")
  202.    "the rain in Spain.the rain in Spain."
  203.    ? (double '(a b c))
  204.    (a b c a b c)
  205.  
  206. Page 43
  207.  
  208.    ? (define-method show-rest (a #rest b)
  209.        (print a)
  210.        (print b)
  211.        #t)
  212.    show-rest
  213.    ? (show-rest 10 20 30 40)
  214.    10
  215.    (20 30 40)
  216.    #t
  217.    ? (show-rest 10)
  218.    10
  219.    ()
  220.    #t
  221.  
  222. Page 44
  223.  
  224.    (define-method percolate (#key (brand 'maxwell-house)
  225.                                   (cups 4)
  226.                                   (strength 'strong))
  227.      (make-coffee brand cups strength))
  228.    (define-method layout (widget #key (position: the-pos)
  229.                                       (size: the-size))
  230.      (bind ((the-sibling (sibling widget)))
  231.       (unless (= the-pos (position the-sibling))
  232.         (align-objects widget the-sibling the-pos the-size))
  233.  
  234. Page 44
  235.  
  236.    (percolate brand: 'folgers cups: 10)
  237.    (percolate strength: 'weak
  238.               brand: 'tasters-choice
  239.               cups: 1)
  240.    (layout my-widget position: (point 10 10)
  241.                      size: (point 30 50))
  242.    (layout my-widget size: (query-user-for-size))
  243.  
  244. Page 45
  245.  
  246.    ? (define-method show-keys (req1 req2 #key foo)
  247.        (format #t "requireds: ~a ~a~%" req1 req2)
  248.        (format #t "key: ~a" foo)
  249.        #t)
  250.    show-keys
  251.    ? (show-keys 'one 'two foo: 'three)
  252.    requireds: one two
  253.    key: three
  254.    #t
  255.    ? (show-keys foo: 'three)
  256.    requireds: foo: three
  257.    key: #f
  258.    #t
  259.  
  260. Page 46
  261.  
  262.    ? (define-method label ((x <object>) #key price)
  263.       (list price x))
  264.    label
  265.    ? (define-method label ((x <sequence>) #key unit-price)
  266.       (add x (* unit-price (length x))))
  267.    label
  268.    ? (define-method label ((x <list>) #rest info #key calories)
  269.       (add x calories))
  270.    label
  271.    ? (label 'grape price: 189 unit-price: 2)
  272.    error:  illegal keyword argument unit-price:.  Accepted keyword arguments are (price:).
  273.    ? (label 'grape price: 189)
  274.    (189 grape)
  275.    ? (label (vector 3 4 5) price: 189 unit-price: 2)
  276.    #(6 3 4 5)
  277.    ? (label (vector 3 4 5) protein: 7 fat: 8 calories: 9)
  278.    error:  illegal keyword argument protein:.  Accepted keyword arguments are (price: unit-price:).
  279.    ? (label (list 3 4 5) protein: 7 fat: 8 calories: 9)
  280.    (9 3 4 5)
  281.  
  282. Page 46
  283.  
  284.    ? (define-method test (the-req #rest the-rest
  285.                                   #key a b)
  286.        (print the-req)
  287.        (print the-rest)
  288.        (print a)
  289.        (print b))
  290.    test
  291.    ? (test 1 a: 2 b: 3 c: 4)
  292.    1
  293.    (a: 2 b: 3 c: 4)
  294.    2
  295.    3
  296.  
  297. Page 49
  298.  
  299.    (define-class <point> (<object>)
  300.      horizontal
  301.      vertical)
  302.  
  303. Page 49
  304.  
  305.    (horizontal my-point)
  306.  
  307. Page 49
  308.  
  309.    ((setter horizontal) my-point 10)
  310.  
  311. Page 50
  312.  
  313.    (set! (horizontal my-point) 10)
  314.  
  315. Page 51   
  316.    
  317.    ? (define-class <menu> (<object>)
  318.        title
  319.        action)
  320.  
  321. Page 55
  322.  
  323.    ? (define-class <rectangle> (<object>)
  324.         (top type: <integer>
  325.              init-value: 0
  326.              init-keyword: top:)
  327.         (left type: <integer>
  328.               init-value: 0
  329.               init-keyword: left:)
  330.         (bottom type: <integer>
  331.                 init-value: 100
  332.                 init-keyword: bottom:)
  333.         (right type: <integer>
  334.                init-value: 100
  335.                init-keyword: right:))
  336.    <rectangle>
  337.    ? <rectangle>
  338.    {the class <rectangle>}
  339.    ? (define my-rectangle (make <rectangle> top: 50 left: 50))
  340.    my-rectangle
  341.    ? (top my-rectangle)
  342.    50
  343.    ? (bottom my-rectangle)
  344.    100
  345.    ? (set! (bottom my-rectangle) 55)
  346.    55
  347.    ? (bottom my-rectangle)
  348.    55
  349.    ? (set! (bottom my-rectangle) 'foo)
  350.    error: foo is not an instance of <integer> while executing (setter bottom).
  351.  
  352.  
  353. Page 58
  354.    
  355.    (define-class <view> (<object>)
  356.      (position allocation: instance)
  357.      ...)
  358.    
  359.    (define-class <displaced-view> (<view>)
  360.      (position allocation: virtual)
  361.      ...)
  362.    
  363.    (define-method position ((v <displaced-view>))
  364.      (displace-transform (next-method v)))
  365.    
  366.    (define-method (setter position) ((v <displaced-view>)
  367.                                      new-position)
  368.      (next-method v (undisplace-transform new-position)))
  369.  
  370. Page 59
  371.  
  372.    (define-class <shape> (<view>)
  373.      (image allocation: virtual)
  374.      (cached-image allocation: instance init-value: #f)
  375.      ...)
  376.    
  377.    (define-method image ((shape <shape>))
  378.      (or (cached-image shape)
  379.          (set! (cached-image shape) (compute-image shape))))
  380.    
  381.    (define-method (setter image) ((shape <shape>) new-image)
  382.      (set! (cached-image shape) new-image))
  383.  
  384. Page 61
  385.  
  386.    ? (define foo 10)
  387.    10
  388.    ? foo             ;this is a variable
  389.    10                ;this is the variable's contents
  390.    ? (set! foo (+ 10 10))
  391.    20
  392.    ? foo
  393.    20
  394.    ? (setter element)                   ;this is a variable
  395.    {generic function (setter element)}  ;the variable's contents
  396.    ? (set! (setter element) %set-element)
  397.    {primitive function %set-element}
  398.    ? (id? (setter element) %set-element)
  399.    #t
  400.  
  401. Page 62
  402.  
  403.    ? (define foo (vector 'a 'b 'c 'd))
  404.    foo
  405.    ? foo
  406.    #(a b c d)
  407.    ? (element foo 2)
  408.    c
  409.    ? (set! (element foo 2) 'sea)
  410.    sea
  411.    ? (element foo 2)
  412.    sea
  413.    ? foo
  414.    #(a b sea d)
  415.  
  416. Page 64
  417.  
  418.    ? (define-method test ((thing <object>))
  419.        (if thing
  420.            #t
  421.            #f))
  422.    test
  423.    ? (test 'hello)
  424.    #t
  425.    ? (test #t)
  426.    #t
  427.    ? (test #f)
  428.    #f
  429.    
  430.    ? (define-method double-negative ((num <number>))
  431.         (if (< num 0)
  432.             (+ num num)
  433.             num))
  434.    double-negative
  435.    ? (double-negative 11)
  436.    11
  437.    ? (double-negative -11)
  438.    -22
  439.  
  440. Page 65
  441.  
  442.    ? (define-method show-and-tell ((thing <object>))
  443.         (if thing
  444.             (begin
  445.                (print thing)
  446.                #t)
  447.             #f))
  448.    show-and-tell
  449.    ? (show-and-tell "hello")
  450.    hello
  451.    #t
  452.  
  453. Page 65
  454.  
  455.    (when (bonus-illuminated? pinball post)
  456.        (add-bonus-score current-player 100000))
  457.  
  458. Page 65
  459.  
  460.    (unless (detect-gas? nose)
  461.        (light match))
  462.  
  463. Page 66
  464.    
  465.    (cond ((< new-position old-position)
  466.             "the new position is less")
  467.           ((= new-position old-position)
  468.            "the positions are equal")
  469.           (else: "the new position is greater"))
  470.  
  471. Page 67
  472.  
  473.    (case (career-choice student)
  474.       ((art music drama)
  475.        (print "Don't quit your day job."))
  476.       ((literature history linguistics)
  477.        (print "That really is fascinating."))
  478.       ((science math engineering)
  479.        (print "Say, can you fix my VCR?"))
  480.       (else: "I wish you luck."))
  481.  
  482. Page 67
  483.  
  484.    (select my-object instance?
  485.      ((<window> <view> <rectangle>) "it's a graphic object")
  486.      ((<number> <list> <sequence>) "it's something computational")
  487.      (else: "Don't know what it is"))
  488.  
  489. Page 68
  490.  
  491.    ? (if #t
  492.          (print "it was true")
  493.          #t
  494.          #f)
  495.    error:  too many arguments to if.
  496.    ? (if #t
  497.          (begin (print "it was true")
  498.                 #t)
  499.          #f)
  500.    "it was true"
  501.    #t
  502.  
  503. Page 69
  504.  
  505.    (define-method factorial ((n <integer>))
  506.       (for ((i n (- i 1))   ;variable clause 1
  507.             (v 1 (* v i)))  ;variable clause 2
  508.            ((<= i 0) v))    ;end test and result
  509.  
  510. Page 69
  511.  
  512.    (define-method first-even ((s <sequence>))
  513.      (for-each ((number s))
  514.                ((even? number) number)
  515.                                 ; No body forms needed
  516.         ))
  517.  
  518. Page 70
  519.  
  520.    (define-method schedule-olympic-games ((cities <sequence>)
  521.                                           (start-year <number>))
  522.       (for-each ((year (range from: start-year by: 4))
  523.                  (city cities))
  524.                 ()              ; No end test needed.
  525.          (schedule-game city year)))
  526.  
  527. Page 70
  528.  
  529.    ? (begin
  530.        (dotimes (i 6) (print "bang!"))
  531.        (print "click!"))
  532.    bang!
  533.    bang!
  534.    bang!
  535.    bang!
  536.    bang!
  537.    bang!
  538.    click!
  539.  
  540. Page 71
  541.  
  542.    ? (define-method first-even ((seq <sequence>))
  543.        (bind-exit (exit)
  544.          (do (method (item)
  545.                 (when (even? item)
  546.                   (exit item)))
  547.               seq)))
  548.    first-even
  549.    ? (first-even '(1 3 5 4 7 9 10))
  550.    4
  551.  
  552. Page 72
  553.  
  554.    ? +
  555.    {the generic function +}
  556.    ? '+
  557.    +
  558.    ? (quote +)
  559.    +
  560.    ? ''+
  561.    (quote +)
  562.    ? (+ 10 10)
  563.    20
  564.    ? '(+ 10 10)
  565.    (+ 10 10)
  566.    ? (quote (+ 10 10))
  567.    (+ 10 10)
  568.  
  569. Page 73
  570.  
  571.    ? (apply + 1 '(2 3))
  572.    6
  573.    ? (+ 1 2 3)
  574.    6
  575.    ? (define math-functions (list + * /  ))
  576.    math-functions
  577.    ? math-functions
  578.    ({method +} {method *} {method /} {method  })
  579.    ? (first math-functions)
  580.    {method +}
  581.    ? (apply (first math-functions) 1 2 '(3 4))
  582.    10
  583.  
  584. Page 79
  585.  
  586.    ? (method (num1 num2)
  587.        (+ num1 num2))
  588.    {an anonymous method}
  589.  
  590. Page 80
  591.  
  592.    ;the second argument to SORT is the test function
  593.    ? (sort person-list
  594.            (method (person1 person2)
  595.              (< (age person1)
  596.                 (age person2))))
  597.    ? (bind ((double (method (number)
  598.                       (+ number number))))
  599.        (double (double 10)))
  600.    40
  601.  
  602. Page 80
  603.  
  604.    ? (define-method double ((my-method <function>))
  605.        (method (#rest args)
  606.          (apply my-method args)
  607.          (apply my-method args)
  608.          #f))
  609.    double
  610.    ? (define print-twice (double print))
  611.    print-twice
  612.    ? print-twice
  613.    {an anonymous method}
  614.    ? (print-twice "The rain in Spain. . .")
  615.    The rain in Spain. . .The rain in Spain. . .
  616.    #f
  617.    ? (print-twice 55)
  618.    5555
  619.    #f
  620.  
  621. Page 81
  622.  
  623.    ? (define-method root-mean-square ((s <sequence>))
  624.         (bind-methods ((average (numbers)
  625.                          (/ (reduce1 + numbers)
  626.                             (length numbers)))
  627.                        (square (n) (* n n)))
  628.            (sqrt (average (map square s)))))
  629.    root-mean-square
  630.    ? (root-mean-square '(5 6 6 7 4))
  631.    5.692099788303083
  632.  
  633. Page 81
  634.  
  635.    ? (define-method newtons-sqrt (x)
  636.         (bind-methods ((sqrt1 (guess)
  637.                           (if (close? guess) 
  638.                               guess 
  639.                               (sqrt1 (improve guess))))
  640.                        (close? (guess)
  641.                           (< (abs (- (* guess guess) x)) .0001))
  642.                        (improve (guess)
  643.                           (/ (+ guess (/ x guess)) 2)))
  644.                (sqrt1 1)))
  645.    newtons-sqrt
  646.    ? (newtons-sqrt 25)
  647.    5.000000000053723
  648.  
  649. Page 82
  650.  
  651.    ? (define-method double ((thing <number>))
  652.        (+ thing thing))
  653.    double
  654.  
  655. Page 82
  656.  
  657.    ? (double 10)
  658.    20
  659.    ? (double 4.5)
  660.    9.0
  661.  
  662. Page 82
  663.  
  664.    ? (define-method double ((thing <integer>))
  665.        (* thing 2))
  666.    double
  667.  
  668. Page 82
  669.  
  670.    ? (define-method double ((thing (singleton 'cup)))
  671.        'pint)
  672.    double
  673.    ? (double 'cup)
  674.    pint
  675.  
  676. Page 83
  677.  
  678.    ? (define-method double ((num <float>))
  679.        (print "doubling a floating-point number")
  680.        (next-method))
  681.    double
  682.    ? (double 10.5)
  683.    doubling a floating-point number
  684.    21.0
  685.  
  686. Page 85
  687.  
  688.    (define-method show ((device <window>) (thing <character>))
  689.      ...)
  690.    
  691.    (define-method show ((device <window>) (thing <string>))
  692.      ...)
  693.    
  694.    (define-method show ((device <window>) (thing <rectangle>))
  695.      . . .)
  696.    
  697.    (define-method show ((device <file>) (thing <character>))
  698.      . . .)
  699.    
  700.    (define-method show ((device <file>) (thing <string>))
  701.      . . .)
  702.  
  703. Page 86
  704.  
  705.    ? (make <generic-function> required: 3)
  706.    {an anonymous generic function}
  707.    ? (make <generic-function> required: 3
  708.                               debug-name: 'foo)
  709.    {the generic function foo}
  710.    ? (define expand
  711.        (make <generic-function> required: 1 debug-name: 'expand))
  712.    {the generic function expand}
  713.    ? (expand 55)
  714.    error: no applicable method for 55 in {the generic function expand}
  715.  
  716. Page 97
  717.  
  718.    ? (define-method double ((thing (singleton 'cup)))
  719.        'pint)
  720.    double
  721.    ? (double 'cup)
  722.    pint
  723.    ? (double 10)
  724.    20
  725.  
  726. Page 98
  727.  
  728.    ? (define-method factorial ((num <integer>))
  729.        (* num (factorial (- num 1))))
  730.    factorial
  731.    ? (define-method factorial ((num (singleton 0)))
  732.         1)
  733.    factorial
  734.    ? (factorial 5)
  735.    120
  736.  
  737. Page 100
  738.  
  739.    ? (do (method (a b) (print (+ a b)))
  740.          '(100 100 200 200)
  741.          '(1 2 3 4))
  742.    101
  743.    102
  744.    203
  745.    204
  746.    #f
  747.  
  748. Page 101
  749.  
  750.    ? (map +
  751.          '(100 100 200 200)
  752.          '(1 2 3 4))
  753.    (101 102 203 204)
  754.  
  755. Page 101
  756.  
  757.    ? (map-as <vector> +
  758.          '(100 100 200 200)
  759.          '(1 2 3 4))
  760.    #(101 102 203 204)
  761.  
  762. Page 101
  763.  
  764.    ? (define x '(100 100 200 200))
  765.    x
  766.    ? (map-into x + '(1 2 3 4))
  767.    (101 102 203 204)
  768.    ? x
  769.    (101 102 203 204)
  770.  
  771. Page 102
  772.  
  773.    ? (any? > '(1 2 3 4) '(5 4 3 2))
  774.    #t
  775.    ? (any? even? '(1 3 5 7))
  776.    #f
  777.  
  778. Page 102
  779.  
  780.    ? (every? > '(1 2 3 4) '(5 4 3 2))
  781.    #f
  782.    ? (every? odd? '(1 3 5 7))
  783.    #t
  784.  
  785. Page 102
  786.  
  787.    ? (define high-score 10)
  788.    high-score
  789.    ? (reduce max high-score '(3 1 4 1 5 9))
  790.    10
  791.    ? (reduce max high-score '(3 12 9 8 8 6))
  792.    12
  793.  
  794. Page 103
  795.  
  796.    ? (reduce1 + '(1 2 3 4 5))
  797.    15
  798.  
  799. Page 103
  800.  
  801.    ? (define flavors #(chocolate pistachio pumpkin))
  802.    flavors
  803.    ? (member? 'chocolate flavors)
  804.    #t
  805.    ? (member? 'banana flavors)
  806.    #f
  807.  
  808. Page 103
  809.  
  810.    ? flavors
  811.    (chocolate pistachio pumpkin)
  812.    ? (find-key flavors has-nuts?)
  813.    1
  814.    ? (element flavors 1)
  815.    pistachio
  816.  
  817. Page 104
  818.  
  819. ? (define numbers (list 10 13 16 19))
  820. numbers
  821. ? (replace-elements! numbers odd? double)
  822. (10 26 16 38)
  823.  
  824. Page 104
  825.  
  826. ? (define x (list 'a 'b 'c 'd 'e 'f))
  827. x
  828. ? (fill! x 3 start: 2)
  829. (a b 3 3 3 3)
  830.  
  831. Page 105
  832.  
  833.    ? (define numbers '(3 4 5))
  834.    numbers
  835.    ? (add numbers 1)
  836.    (1 3 4 5)
  837.    ? numbers
  838.    (3 4 5)
  839.  
  840. Page 105
  841.  
  842.    ? (define numbers (list 3 4 5))
  843.    numbers
  844.    ? (add! numbers 1)
  845.    (1 3 4 5)
  846.  
  847. Page 105
  848.  
  849.    ? (add-new '(3 4 5) 1)
  850.    (1 3 4 5)
  851.    ? (add-new '(3 4 5) 4)
  852.    (3 4 5)
  853.  
  854. Page 105
  855.  
  856.    ? (add-new! (list 3 4 5) 1)
  857.    (1 3 4 5)
  858.    ? (add-new! (list 3 4 5) 4)
  859.    (3 4 5)
  860.  
  861. Page 106
  862.  
  863.    ? (remove '(3 1 4 1 5 9) 1)
  864.    (3 4 5 9)
  865.  
  866. Page 106
  867.  
  868.    ? (remove! (list 3 1 4 1 5 9) 1)
  869.    (3 4 5 9)
  870.  
  871. Page 106
  872.  
  873.    ? (choose even? '(3 1 4 1 5 9))
  874.    (4)
  875.  
  876. Page 106
  877.  
  878.    ? (choose-by even? (range from: 1)
  879.                       '(a b c d e f g h i))
  880.    (b d f h)
  881.  
  882. Page 107
  883.  
  884.    ? (intersection '(john paul george ringo)
  885.                    '(richard george edward charles john))
  886.    (john george)
  887.  
  888. Page 107
  889.  
  890.    ? (union '(butter flour sugar salt eggs)
  891.             '(eggs butter mushrooms onions salt))
  892.    (salt butter flour sugar eggs mushrooms onions)
  893.  
  894. Page 107
  895.  
  896.    ? (remove-duplicates '(spam eggs spam sausage spam spam spam))
  897.    (spam eggs sausage)
  898.  
  899. Page 108
  900.  
  901.    ? (remove-duplicates! '(spam eggs spam sausage spam spam))
  902.    (spam eggs sausage)
  903.    
  904. Page 108
  905.    
  906.    ? (define hamlet '(to be or not to be))
  907.    hamlet
  908.    ? (id? hamlet (copy-sequence hamlet))
  909.    #f
  910.    ? (copy-sequence hamlet start: 2 end: 4)
  911.    (or not)
  912.  
  913. Page 108
  914.  
  915.    ? (concatenate-as <string> '(#\n #\o #\n) '(#\f #\a #\t))
  916.    "nonfat"
  917.    ? (concatenate-as <vector> '(0 1 2) '(3 4 5) '(6 7 8))
  918.    #(0 1 2 3 4 5 6 7 8)
  919.  
  920. Page 108
  921.  
  922.    ? (concatenate "low-" "calorie")
  923.    "low-calorie"
  924.    ? (concatenate '(0 1 2) '(3 4 5) '(6 7 8))
  925.    (0 1 2 3 4 5 6 7 8)
  926.  
  927. Page 109
  928.  
  929.    ? (define phrase "I hate oatmeal.")
  930.    phrase
  931.    ? (replace-subsequence! phrase "like" start: 2)
  932.    "I like oatmeal."
  933.  
  934.  
  935. Page 109
  936.  
  937.    ? (define x '(bim bam boom))
  938.    x
  939.    ? (reverse x)
  940.    (boom bam bim)
  941.    ? x
  942.    (bim bam boom)
  943.  
  944.  
  945. Page 109
  946.  
  947.    ? (reverse! '(bim bam boom))
  948.    (boom bam bim)
  949.  
  950. Page 110
  951.  
  952.    ? (define numbers '(3 1 4 1 5 9))
  953.    numbers
  954.    ? (sort numbers)
  955.    (1 1 3 4 5 9)
  956.    ? numbers
  957.    (3 1 4 1 5 9)
  958.  
  959. Page 110
  960.  
  961.    ? (sort! '(3 1 4 1 5 9))
  962.    (1 1 3 4 5 9)
  963.  
  964. Page 110
  965.  
  966.    ? (last '(emperor of china))
  967.    china
  968.  
  969. Page 111
  970.  
  971.    ? (subsequence-position "Ralph Waldo Emerson" "Waldo")
  972.    6
  973.  
  974. Page 113
  975.  
  976.    ? (aref #(7 8 9) 1)
  977.    8
  978.  
  979. Page 113
  980.  
  981.    ? (set! (aref #(7 8 9) 1) 5)
  982.    #(7 5 9)                        ;buggy example.  Should return 5
  983.    ? ((setter aref) #(7 8 9) 1 5)
  984.    #(7 5 9)                        ;buggy example.  Should return 5
  985.  
  986. Page 113
  987.  
  988.    ? (dimensions (make <array> dimensions: '(4 4)))
  989.    (4 4)
  990.  
  991. Page 115
  992.  
  993.    ? (cons 1 2)
  994.    (1 . 2)
  995.    ? (cons 1 '(2 3 4 5))
  996.    (1 2 3 4 5)
  997.  
  998. Page 115
  999.  
  1000.    ? (list 1 2 3)
  1001.    (1 2 3)
  1002.    ? (list (+ 4 3) (- 4 3))
  1003.    (7 1)
  1004.  
  1005. Page 115
  1006.  
  1007.    ? (list* 1 2 3 '(4 5 6))
  1008.    (1 2 3 4 5 6)
  1009.  
  1010.  
  1011. Page 116
  1012.  
  1013.    ? (car '(4 5 6))
  1014.    4
  1015.    ? (car '())
  1016.    ()
  1017.  
  1018. Page 116
  1019.  
  1020.    ? (cdr '(4 5 6))
  1021.    (5 6)
  1022.    ? (cdr '())
  1023.    ()
  1024.  
  1025. Page 116
  1026.  
  1027.    ? (define x '(4 5 6))
  1028.    (4 5 6)
  1029.    ? (set! (car x) 9)
  1030.    9
  1031.  
  1032. Page 116
  1033.  
  1034.    ? (define x '(4 5 6))
  1035.    (4 5 6)
  1036.    ? (set! (cdr x) '(a b c))
  1037.    (a b c)
  1038.  
  1039. Page 120
  1040.  
  1041.    ? (define x "Van Gogh")
  1042.    x
  1043.    ? (as-lowercase x)
  1044.    "van gogh"
  1045.  
  1046. Page 120
  1047.  
  1048.    ? (define x "Van Gogh")
  1049.    x
  1050.    ? (as-lowercase! x)
  1051.    "van gogh"
  1052.  
  1053. Page 120
  1054.  
  1055.    ? (define x "Van Gogh")
  1056.    x
  1057.    ? (as-uppercase x)
  1058.    "VAN GOGH"
  1059.  
  1060. Page 120
  1061.  
  1062.    ? (define x "Van Gogh")
  1063.    x
  1064.    ? (as-uppercase x)
  1065.    "VAN GOGH"
  1066.  
  1067. Page 123
  1068.  
  1069.    (define-method do1 (f (c <collection>))
  1070.      (for ((state (initial-state c) (next-state c state)))
  1071.           ((not state) #f)
  1072.        (f (current-element c state))))
  1073.  
  1074. Page 125
  1075.  
  1076.    (define-method key-sequence ((c <explicit-key-collection>))
  1077.      (for ((state (initial-state c) (next-state c state))
  1078.            (keys  '()               (cons (current-key c state)
  1079.                                           keys)))
  1080.           ((not state) keys)))
  1081.  
  1082. Page 125
  1083.  
  1084.    (define-method do-with-keys (f (c <explicit-key-collection>))
  1085.      (for ((state (initial-state c) (next-state c state)))
  1086.           ((not state) #f)
  1087.        (f (current-key c state) (current-element c state))))
  1088.  
  1089. Page 126
  1090.  
  1091.    (define-method do-with-keys (f (c <sequence>))
  1092.      (for ((state (initial-state c) (next-state c state))
  1093.            (key   0                 (+ key 1)))
  1094.           ((not state) #f)
  1095.        (f key (current-element c state))))
  1096.  
  1097. Page 126
  1098.  
  1099.    (bind ((no-default (cons #f #f)))
  1100.    
  1101.     (define-method .i.element; ((c <explicit-key-collection>) key
  1102.                             #key (default no-default))
  1103.      (for ((state (initial-state c) (next-state c state)))
  1104.           ((or (not state) (= (current-key c state) key))
  1105.            (if state (current-element c state)
  1106.                (if (id? default no-default)
  1107.                    (error ...)
  1108.                    default)))))
  1109.     (define-method .i.element; ((c <sequence>) key
  1110.                             #key (default no-default))
  1111.       (for ((state (initial-state c) (next-state c state))
  1112.             (k     0                 (+ k 1)))
  1113.            ((or (not state) (= k key))
  1114.             (if state (current-element c state)
  1115.                 (if (id? default no-default)
  1116.                     (error ...)
  1117.                     default))))) )
  1118.  
  1119. Page 128
  1120.  
  1121.    (define-method (setter element) ((c <mutable-sequence>)
  1122.                                     (key <integer>) new-value)
  1123.      (for ((state (initial-state c) (next-state c state))
  1124.            (k     0                 (+ k 1)))
  1125.           ((or (not state) (= k key))
  1126.            (if state
  1127.                (set! (current-element c state) new-value)
  1128.                (error ...)))))
  1129.  
  1130. Page 128
  1131.  
  1132.    (define-method (setter element) ((c <mutable-explicit-key-collection>)
  1133.                                     key new-value)
  1134.      (for ((state (initial-state c) (next-state c state)))
  1135.           ((or (not state) (= (current-key c state) key))
  1136.            (if state
  1137.                (set! (current-element c state) new-value)
  1138.                (error ...)))))
  1139.  
  1140. Page 129
  1141.  
  1142.    (define-method do2 (f (c1 <collection>) (c2 <collection>))
  1143.      (bind ((keys (intersection (key-sequence c1)
  1144.                                 (key-sequence c2))))
  1145.        (for ((ks (initial-state keys) (next-state keys ks)))
  1146.             ((not ks) #f)
  1147.          (bind ((key (current-element keys ks)))
  1148.            (f (element c1 key) (element c2 key))))))
  1149.  
  1150. Page 129
  1151.  
  1152.    (define-method do2 (f (c1 <sequence>) (c2 <sequence>))
  1153.      (for ((s1 (initial-state c1) (next-state c1 s1))
  1154.            (s2 (initial-state c2) (next-state c2 s2)))
  1155.           ((or (not s1) (not s2)) #f)
  1156.        (f (current-element c1 s1) (current-element c2 s2))))
  1157.  
  1158. Page 130
  1159.  
  1160.    (define-method map-into1 ((target <mutable-collection>) f
  1161.                              (source <collection>))
  1162.      (bind ((keys (intersection (key-sequence target)
  1163.                                 (key-sequence source))))
  1164.        (for ((ks (initial-state keys) (next-state keys ks)))
  1165.             ((not ks) target)
  1166.          (bind ((key (current-element keys ks)))
  1167.            (set! (element target key) (f (element source key)))))))
  1168.    (define-method map-into1 ((target <mutable-sequence>) f
  1169.                              (source <sequence>))
  1170.      (for ((ss (initial-state source) (next-state source ss))
  1171.            (ts (initial-state target) (next-state target ts)))
  1172.           ((or (not ss) (not ts)) target)
  1173.        (set! (current-element target ts)
  1174.              (f (current-element source ss)))))
  1175.  
  1176. Page 142
  1177.  
  1178.    (handler-case (some-function)
  1179.      ((<type-error>) "there was a type-error")
  1180.      ((<error>) "there was an error")
  1181.      ((<warning>) "there was a warning"))
  1182.  
  1183. Page 144-146
  1184.  
  1185.    ;;; Classes such as <file-not-found> used in these examples are
  1186.    ;;; invented for the example and are not part of the specification
  1187.    ;;; This example shows minimal handling of a file-not-found error
  1188.    
  1189.    (handler-case (open "file-that-doesnt-exist")
  1190.      ((<file-not-found> condition: c
  1191.        (format *error-output* "~&The file ~A was not found."
  1192.                (file-name c))))
  1193.    
  1194.    
  1195.    ;;; This example shows how to handle a file-not-found error by
  1196.    ;;; reading a different file instead.
  1197.    (handler-bind (<file-not-found>
  1198.                     (method (condition next-handler)
  1199.                       (signal (make <try-a-different-file>
  1200.                                     file-name: "my-emergency-backup-file"))))
  1201.       (open "file-that-doesnt-exist")
  1202.      ....)
  1203.    
  1204.    (define-method open (the-file)
  1205.      (handler-case (guts-of-open the-file)
  1206.        ((<try-a-different-file>
  1207.          description: (method (stream)
  1208.                         (format stream "Read a different file instead of ~A"
  1209.                                         the-file))
  1210.          condition: restart
  1211.         (open (file-name restart)))))))
  1212.    
  1213.    (define-method guts-of-open (the-file)
  1214.      (bind ((result (operating-system-open the-file)))
  1215.        (cond ((instance? result <stream>) result)
  1216.              ((id? result +file-not-found-error-code+)
  1217.               (error (make <file-not-found> file-name: the-file)))
  1218.              ...)))
  1219.    
  1220.    (define-class <file-not-found> (<error>)
  1221.      ((file-name init-keyword: file-name:)))
  1222.    
  1223.    (define-method print ((self <file-not-found>) #key stream verbose)
  1224.      (if verbose
  1225.          (next-method)
  1226.          (format stream "The file ~A was not found" (file-name self))))
  1227.    
  1228.    (define-class <try-a-different-file> (<restart>)
  1229.      ((file-name init-keyword: file-name:)))
  1230.    
  1231.    
  1232.    ;;; This is the same example improved so the restart handler that
  1233.    ;;; reads another file can only be reached by a handler for the
  1234.    ;;; associated condition, useful if there are nested errors.
  1235.    
  1236.    (handler-bind (<file-not-found>)
  1237.                     (method (condition next-handler)
  1238.                       (signal (make <try-a-different-file>
  1239.                                     condition: condition
  1240.                                     file-name: "my-emergency-backup-file")))
  1241.      (open "file-that-doesnt-exist")
  1242.      ....)
  1243.    
  1244.    (define-method open (the-file)
  1245.      ....  (guts-of-open the-file))
  1246.    
  1247.    (define-method guts-of-open (the-file)
  1248.      (bind ((result (operating-system-open the-file)))
  1249.        (cond ((instance? result <stream>) result)
  1250.              ((id? result +file-not-found-error-code+)
  1251.               (bind ((condition (make <file-not-found> file-name: the-file)))
  1252.                 (handler-case (error condition)
  1253.                   ((<try-a-different-file>
  1254.                      test: (compose (curry id? condition) restart-condition)
  1255.                     description: (method (stream)
  1256.                                    (format stream
  1257.                                      "Read a different file instead of ~A"
  1258.                                      the-file))
  1259.                     condition: restart
  1260.                    (open (file-name restart)))))))
  1261.              ...)))
  1262.    
  1263.    (define-class <file-not-found> (<error>)
  1264.      ((file-name init-keyword: file-name:)))
  1265.    
  1266.    (define-method print ((self <file-not-found>) #key stream verbose)
  1267.      (if verbose
  1268.         (next-method)
  1269.         (format stream "The file ~A was not found" (file-name self))))
  1270.    
  1271.    (define-class <try-a-different-file> (<restart>)
  1272.      ((condition init-keyword: condition: reader: restart-condition)
  1273.       (file-name init-keyword: file-name:)))
  1274.  
  1275. Page 153
  1276.  
  1277.    ? (as <symbol> "foo")
  1278.    foo
  1279.    ? (id? 'FOO (as <symbol> "Foo"))
  1280.    #t
  1281.    ? 'Foo
  1282.    foo
  1283.    ? (as <keyword> "foo")
  1284.    foo:
  1285.  
  1286. Page 154
  1287.  
  1288.    ? (as <string> 'Foo)
  1289.    "foo"
  1290.    ? (as <string> 'bar:)
  1291.    "bar"
  1292.  
  1293. Page 157
  1294.  
  1295.    ? (define-method sum ((numbers <sequence>))
  1296.          (reduce1 + numbers))
  1297.    sum
  1298.    ? (define-method square ((x <number>)) (* x x))
  1299.    square
  1300.    ? (define-method square-all ((coords <sequence>))
  1301.        (map square coords))
  1302.    square-all
  1303.    ? (define distance (compose sqrt sum square-all))
  1304.    distance
  1305.    ? (distance '(3 4 5))
  1306.    7.0710678118654755
  1307.  
  1308. Page 157
  1309.  
  1310.    ? (map female? '(michelle arnold roseanne))
  1311.    (#t #f #t)
  1312.    ? (map (complement female?) '(michelle arnold roseanne))
  1313.    (#f #t #f)
  1314.  
  1315. Page 158
  1316.  
  1317.    ? (map (curry + 1) '(3 4 5))
  1318.    (4 5 6)
  1319.  
  1320. Page 158
  1321.  
  1322.    ? (define yuppify (rcurry concatenate ", ayup"))
  1323.    yuppify
  1324.    ? (yuppify "I'm from New Hampsha")
  1325.    "I'm from New Hampsha, ayup"
  1326.  
  1327. Page 159
  1328.  
  1329.    ? ((always 1) 'x 'y 'z)
  1330.    1
  1331.    ? ((always #t) #f #f)
  1332.    #t
  1333.  
  1334. $Id: examples-from-book.text,v 1.3 1992/09/25 13:47:57 birkholz Exp $
  1335.